package com.fw.ui.view.settings.changepassword;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bean.AuthenticationBean;
import com.fw.ui.core.ViewDescription;
import com.fw.ui.core.authentication.AuthenticationUserToken;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractView;
import com.fw.ui.view.common.ChangePasswordPanel;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
@ViewDescription(name = "settings-change-password", path = "/settings/change-password")
public class ChangePasswordView extends AbstractView {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private MessageBAO messageBAO;

    /**
     * COMPONENT
     */
    @Autowired
    private ChangePasswordPanel changePasswordPanel;

    private Panel changePasswordViewPanel = new Panel();

    @Override
    public void updateStrings() {
	this.changePasswordPanel.setCaption(this.messageBAO.getMessage("view.settings-change-password.title"));
    }

    @Override
    public void init() {
	this.setSizeFull();

	this.changePasswordViewPanel.setSizeFull();
	this.changePasswordViewPanel.setId("changePasswordViewPanel");

	changePasswordPanel.setId("changePasswordPanel");
	changePasswordPanel.removeBoxStyle();
	changePasswordPanel.removeHeaderLayout();
	changePasswordPanel.removeFooterLayout();

	VerticalLayout mainLayout = new VerticalLayout();
	mainLayout.setSizeFull();
	mainLayout.setSizeUndefined();
	mainLayout.setSpacing(true);
	mainLayout.setWidth("100%");

	mainLayout.addComponent(changePasswordPanel);

	this.changePasswordViewPanel.setContent(mainLayout);

	this.addComponent(this.changePasswordViewPanel);
	this.setComponentAlignment(this.changePasswordViewPanel, Alignment.TOP_CENTER);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initValidator() {
    }

    @Override
    public void initData() {
	Authentication authentication = (Authentication) SecurityContextHolder.getContext().getAuthentication();
	if (authentication != null && authentication instanceof AuthenticationUserToken) {
	    AuthenticationUserToken userToken = (AuthenticationUserToken) authentication;
	    String oldPassword = userToken.getCredentials().toString();
	    AuthenticationBean authenticationBean = userToken.getAuthenticationBean();
	    this.changePasswordPanel.setUserId(authenticationBean.getUserId());
	    this.changePasswordPanel.setOldPassword(oldPassword);
	}
    }

    public Panel getCreateBOUserPanel() {
	return changePasswordViewPanel;
    }

    public void setCreateBOUserPanel(Panel addSamplePanel) {
	this.changePasswordViewPanel = addSamplePanel;
    }

}
